package com.example.test.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 王雪杰
 * @date 2021/5/24 10:01
 */
@RestController
@Slf4j
public class FileUpload {

    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        if (file == null) {
            System.out.println("multipartFile == null");
        }

        String tmpFile = "E:" + File.separator + "1" + File.separator + newDate + "-" + file.getOriginalFilename();
        File targetFile = new File(tmpFile);
        //保存文件
        try {
            file.transferTo(targetFile);
        } catch (IllegalStateException e) {
            log.error("multipartFile.transferTo, IllegalStateException", e);
        } catch (IOException e) {
            log.error("multipartFile.transferTo, IOException", e);
        }

        return file.getOriginalFilename();
    }

}
